//
//  PhotoViewController.swift
//  XiaoXiangMu1604
//
//  Created by qianfeng on 16/9/27.
//  Copyright © 2016年 SunPiaoLiang. All rights reserved.
//

import UIKit

class PhotoViewController: UIViewController {

    //点击图片的序号
    var photoIndex: Int?
    
    //标题文字
    private var titleLabel: UILabel?
    
    //滚动视图
    private var scrollView: UIScrollView?
    
    //所有图片的网址信息
    var photoArray: Array<PhotoModel>?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        createMyNav()
        
        
        createScrollView()
    
    }
    func createScrollView(){
        
        scrollView = UIScrollView(frame: CGRectMake(0,220,KScreenWidth,300))
        scrollView?.backgroundColor = UIColor.lightGrayColor()
        
        view.addSubview(scrollView!)
        
        //循环添加图片
        let cnt = photoArray?.count
        
        ProgressHUD.showOnView(view)

        for i in 0..<cnt!{
            
            let pModel = photoArray![i]
            
            let tmpImageView = UIImageView(frame: CGRectMake(KScreenWidth*CGFloat(i), 0, (scrollView?.bounds.size.width)!, (scrollView?.bounds.size.height)!))
            
            let url = NSURL(string: pModel.originalUrl!)
            
            if i != photoIndex!{
                
                 tmpImageView.kf_setImageWithURL(url!)
                
            }else{
                
                //进入时，显示的图片
                tmpImageView.kf_setImageWithURL(url!, placeholderImage: nil, optionsInfo: nil, progressBlock: { (receivedSize, totalSize)  in
                    
                     //下载的进度
                    
                    }, completionHandler: { (image, error, cacheType, imageURL)  in
                        
                        //下载完成
                       ProgressHUD.hideOnView(self.view)
                        
                })
            
            
            }
            
            
        
            scrollView?.addSubview(tmpImageView)
        
        }
        
        
        //设置滚动范围
        scrollView?.contentSize = CGSize(width: KScreenWidth*CGFloat(cnt!), height: 0)
        
        scrollView?.pagingEnabled = true
        
        scrollView?.delegate = self
        
        scrollView?.contentOffset = CGPointMake(CGFloat(photoIndex!)*KScreenWidth, 0)
    }
    
    //自己加的类似导航的东西
    func createMyNav(){
        
        //导航的背景图片
        let bgImageView = UIImageView(frame: CGRectMake(0, 20, KScreenWidth, 44))
        
        bgImageView.image = UIImage(named: "navigationbar")
        
        view.addSubview(bgImageView)
        
        //文字
        let title = "第\(photoIndex! + 1)页  共\((photoArray?.count)!)页"
        
        titleLabel = UILabel.createLabelFrame(CGRectMake(80, 0, 215, 44), title: title, textAlignment: .Center)
        
        bgImageView.addSubview(titleLabel!)
        
        let btn = UIButton.createBtn(CGRectMake(290, 4, 60, 36), title: "Done", bgImageName: "buttonbar_action", target: self, action: "doneAction")
        
        bgImageView.addSubview(btn)
        
        bgImageView.userInteractionEnabled = true
        
    }
    func doneAction(){
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //MARK:设置状态栏的样式
    //这种方式是只修改当前视图控制器的状态栏模式
    override func prefersStatusBarHidden() -> Bool {
        
        return false
        
    }
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        
        //在info.plist中设置View controller-based status bar appearance为YES
        (UINavigationBar.appearance()).barStyle = .Black
        //黑底白字
        return .LightContent
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
//MARK:UIScrollView代理
extension PhotoViewController:UIScrollViewDelegate{
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        
        let title = "第\(index + 1)页  共\((photoArray?.count)!)页"
        
        titleLabel?.text = title
        
    }
    
    
}

